package cn.com.lyj6851.modules.order.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.cart.vo.YdyStoreCartQueryVo;
import cn.com.lyj6851.modules.order.domain.YdyStoreOrderCartInfo;
import cn.com.lyj6851.modules.order.service.YdyStoreOrderCartInfoService;
import cn.com.lyj6851.modules.order.service.dto.YdyStoreOrderCartInfoDto;
import cn.com.lyj6851.modules.order.service.dto.YdyStoreOrderCartInfoQueryCriteria;
import cn.com.lyj6851.modules.order.service.mapper.StoreOrderCartInfoMapper;
import cn.com.lyj6851.utils.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdyStoreOrderCartInfoServiceImpl extends BaseServiceImpl<StoreOrderCartInfoMapper, YdyStoreOrderCartInfo> implements YdyStoreOrderCartInfoService {

    private final IGenerator generator;

    @Override
    public YdyStoreOrderCartInfo findByUni(String unique) {
        QueryWrapper<YdyStoreOrderCartInfo> wrapper= new QueryWrapper<>();
        wrapper.eq("`unique`",unique);
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 添加购物车商品信息
     * @param oid 订单id
     * @param cartInfo 购物车信息
     */
    @Override
    public void saveCartInfo(Long oid, List<YdyStoreCartQueryVo> cartInfo) {

        List<YdyStoreOrderCartInfo> list = new ArrayList<>();
        for (YdyStoreCartQueryVo cart : cartInfo) {
            YdyStoreOrderCartInfo info = new YdyStoreOrderCartInfo();
            info.setOid(oid);
            info.setCartId(cart.getId());
            info.setProductId(cart.getProductId());
            info.setCartInfo(JSONObject.toJSON(cart).toString());
            info.setUnique(IdUtil.simpleUUID());
            list.add(info);
        }

        this.saveBatch(list);
    }


    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YdyStoreOrderCartInfoQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyStoreOrderCartInfo> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdyStoreOrderCartInfoDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YdyStoreOrderCartInfo> queryAll(YdyStoreOrderCartInfoQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyStoreOrderCartInfo.class, criteria));
    }


    @Override
    public void download(List<YdyStoreOrderCartInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyStoreOrderCartInfoDto yxStoreOrderCartInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单id", yxStoreOrderCartInfo.getOid());
            map.put("购物车id", yxStoreOrderCartInfo.getCartId());
            map.put("商品ID", yxStoreOrderCartInfo.getProductId());
            map.put("购买东西的详细信息", yxStoreOrderCartInfo.getCartInfo());
            map.put("唯一id", yxStoreOrderCartInfo.getUnique());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
